package com.example.zhilongzhang.constructionpayapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.zhilongzhang.constructionpayapp.R;
import com.example.zhilongzhang.constructionpayapp.base.BaseActivity;

public class ChangeInfoActivity extends BaseActivity implements View.OnClickListener {
    ImageView ivBack,ivCancelAccoout;
    TextView tvInfo;
    EditText etInfo;
    TextView tvFinish;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changeinfo);
        ivBack = findViewById(R.id.left);
        ivCancelAccoout = findViewById(R.id.cancel_accoout);
        tvInfo = findViewById(R.id.tv_info);
        etInfo = findViewById(R.id.et_old_info);
        tvFinish = findViewById(R.id.tv_finish);

        String title = getIntent().getStringExtra("title");
        String content = getIntent().getStringExtra("content");

        tvInfo.setText(title);
        etInfo.setText(content);

        ivBack.setOnClickListener(this);
        ivCancelAccoout.setOnClickListener(this);
        tvFinish.setOnClickListener(this);

        // 显隐
        etInfo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    ivCancelAccoout.setVisibility(View.VISIBLE);
                } else {
                    ivCancelAccoout.setVisibility(View.INVISIBLE);
                }
            }
        });

        // 全部删除
        ivCancelAccoout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etInfo.setText("");
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left:
                finish();
                break;
            case R.id.tv_finish:
                setResult(RESULT_OK, new Intent().putExtra("content", etInfo.getText().toString()));
                finish();
                break;
        }
    }

}
